﻿using System;
using System.Collections.Generic;
using System.Text;
using OpenWings.Storage;
using System.ComponentModel;

namespace OpenWings.Entities
{
    public class Config : Entity
    {
        static Config Instance = new Config();

        public ServerConfig ServerConfig;
        public ClientConfig ClientConfig;

        public string ServersFilePath;
        public string ClientsFilePath;

        Config()
        {
            ServersFilePath = "Servers.xml";
            ClientsFilePath = "Clients.xml";
        }

        public static Config Current
        {
            get { return Instance; }
        }

        public void Load(string pathServer, string pathClient)
        {
            ClientsFilePath = pathClient;
            ClientConfig = new XmlStorage<ClientConfig>(ClientsFilePath).Load();

            ServersFilePath = pathServer;
            ServerConfig = new XmlStorage<ServerConfig>(ServersFilePath).Load();

            ServerConfig.DirtyChanged += new EventHandler<DirtyChangedEventArgs>(ServerOrClientConfig_DirtyChanged);
            ClientConfig.DirtyChanged += new EventHandler<DirtyChangedEventArgs>(ServerOrClientConfig_DirtyChanged);
        }

        void ServerOrClientConfig_DirtyChanged(object sender, DirtyChangedEventArgs e)
        {
            IsDirty = e.Dirty;
        }

        public override void OnDirtyTrackingChanged(object sender, DirtyTrackingChangedEventArgs e)
        {
            ClientConfig.DirtyTracking = e.Enabled;
            ServerConfig.DirtyTracking = e.Enabled;

            base.OnDirtyTrackingChanged(sender, e);
        }

        public void Save()
        {
            new XmlStorage<ServerConfig>(ServersFilePath).Save(ServerConfig);
            new XmlStorage<ClientConfig>(ClientsFilePath).Save(ClientConfig);

            Clean();
        }

        public override void Clean()
        {
            ClientConfig.Clean();
            ServerConfig.Clean();

            base.Clean();
        }
    }
}
